import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter);

//加载条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css' //这个样式必须引入

//查询当前域名下的DSP系统是否提供注册
import { getIsRegister } from '../api/login'

const router = new VueRouter({
    mode: 'hash',
    linkActiveClass: 'active',

    routes: [
        {
            path: '/',
            component: resolve => require(['@/views/Home.vue'], resolve),
            meta: {
                title: '自述文件',
                requireAuth: true
            },
            children: [
                {
                    // 404 路由输出 默认页面
                    path: '/404',
                    component: resolve => require(['@/views/error-page/404.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    path: '/',
                    redirect: '/dashboard',
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    path: '/dashboard',
                    component: resolve => require(['@/views/Dashboard.vue'], resolve),
                    meta: {
                        title: '系统用户管理',
                        requireAuth: true
                    },
                },
                // Campaign_Group
                {
                    // 查询所有广告活动
                    path: '/bidder/campaigns',
                    component: resolve => require(['@/views/bidder/Campaigns.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    // 添加广告活动 原始
                    path: '/bidder/editcampaign',
                    component: resolve => require(['@/views/bidder/EditCampaign.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    // 添加广告活动 点击下拉渲染
                    path: '/bidder/editcampaign2',
                    component: resolve => require(['@/views/bidder/EditCampaign2.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },

                {
                    // 添加广告活动 点击自定义渲染
                    path: '/bidder/editcampaign3',
                    component: resolve => require(['@/views/bidder/EditCampaign3.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                
                {
                    // 查看单条广告活动，进行修改
                    path: '/bidder/editcampaign/id/:id',
                    component: resolve => require(['@/views/bidder/EditCampaign.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                }, {
                    // 查看所有广告系列
                    path: '/bidder/viewcreative/campaignid/:id',
                    component: resolve => require(['@/views/bidder/Creative.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                }, {
                    // 添加广告系列（横幅，横幅视频...）
                    name: 'addCreateCreative',
                    path: '/bidder/editcreative/campaignid/:id',
                    component: resolve => require(['@/views/bidder/EditCreative.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                }, {
                    // 查看单条广告系列
                    name: 'getCreateCreative',
                    path: '/bidder/editcreative/creativeid/:id',
                    component: resolve => require(['@/views/bidder/EditCreative.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    path: '/bidder/filterlist',
                    component: resolve => require(['@/views/bidder/FilterList.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    path: '/bidder/filterlist/create',
                    component: resolve => require(['@/views/bidder/CreateFilterList.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    path: '/bidder/filterlist/history',
                    component: resolve => require(['@/views/bidder/History.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    path: '/bidder/filterlist/view/id/:id',
                    component: resolve => require(['@/views/bidder/ViewFilterList.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    path: '/bidder/creativelibrary',
                    component: resolve => require(['@/views/bidder/CreativeLibrary.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    path: '/bidder/uploadfile/folderid/:id',
                    component: resolve => require(['@/views/bidder/UploadFile.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                // 查看个人信息
                {
                    name: 'profile',
                    /**
                     * @important 如果`修改`该path,记得`同时修改`
                     * @important `src\views\admin\EditUsers.vue`
                     * @important 下的计算属性:
                     * @important `showPersonalPhoto`
                     */
                    path: '/bidder/profile',
                    component: resolve => require(['@/views/admin/EditUsers.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                // reports
                {
                    path: '/reports/:type/:id',
                    component: resolve => require(['@/views/reports/Reports.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                //alerts
                {
                    path: '/reports/alerts',
                    component: resolve => require(['@/views/reports/Alerts.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },

                // BELLING_Group
                {
                    path: '/billing/billing',
                    component: resolve => require(['@/views/billing/Billing.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    path: '/billing/invoice',
                    component: resolve => require(['@/views/billing/Invoice.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    path: '/billing/transactionhistory',
                    component: resolve => require(['@/views/billing/TransactionHistory.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                // ADMIN_Group
                // 分页查询用户信息
                {
                    path: '/admin/users',
                    component: resolve => require(['@/views/admin/ManageUsers.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                // 管理公司样式
                {
                    path: '/admin/stylesettings',
                    component: resolve => require(['@/views/admin/EditStyle.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                // 添加用户
                {
                    path: '/admin/users/save',
                    component: resolve => require(['@/views/admin/EditUsers.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                // 查询单条数据
                {
                    path: '/admin/users/edit/id/:id',
                    component: resolve => require(['@/views/admin/EditUsers.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                // 待审批
                {
                    path: '/admin/pendingapproval',
                    component: resolve => require(['@/views/admin/PendingApproval.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                // 管理交易
                {
                    path: '/admin/managetransactions',
                    component: resolve => require(['@/views/admin/ManageTransactions.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                //管理规则
                {
                    path: '/admin/CampaignsRules',
                    component: resolve => require(['@/views/admin/CampaignsRules.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                //创建规则
                {
                    path: '/admin/editRules',
                    component: resolve => require(['@/views/admin/EditRules.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                {
                    // 查看规则广告活动，进行修改
                    path: '/admin/editRules/id/:id',
                    component: resolve => require(['@/views/admin/EditRules.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                /*{
                    // 查看广告活动下拉框，进行管理
                    path: '/admin/ManageSelect',
                    component: resolve => require(['@/views/admin/ManageSelect.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },*/
                // 供应设置
                {
                    path: '/admin/usersettings',
                    component: resolve => require(['@/views/admin/SupplySettings.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                // Add Transaction ID
                {
                    path: '/admin/edittransaction/:userId',
                    component: resolve => require(['@/views/admin/AddTransactionID.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                // Daily Report Manager
                {
                    path: '/admin/dailyreportmanager',
                    component: resolve => require(['@/views/admin/DailyReportManager.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                // EditDailyReport
                {
                    path: '/admin/editdailyreport/',
                    component: resolve => require(['@/views/admin/EditDailyReport.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                // 总报表
                {
                    path: '/admin/generalreport/',
                    component: resolve => require(['@/views/admin/GeneralReport.vue'], resolve),
                    meta: {
                        requireAuth: true
                    },
                },
                // 总设置
                {
                    path: '/admin/sitesettings/',
                    component: resolve => require(['@/views/admin/sitesettings/Sitesettings.vue'], resolve),
                    meta: { requireAuth: true },
                },
                {
                    path: '/admin/sitesettings/set',
                    component: resolve => require(['@/views/admin/sitesettings/index.vue'], resolve),
                    meta: { requireAuth: true },
                    children: [
                        {
                            path: '/',
                            redirect: '/admin/sitesettings/termsofuse',
                            meta: {
                                requireAuth: true
                            },
                        },
                        {
                            path: '/admin/sitesettings/termsofuse',
                            component: resolve => require(['@/views/admin/sitesettings/Termsofuse.vue'], resolve),
                            meta: { requireAuth: true },
                        },
                        {
                            path: '/admin/sitesettings/privacypolicy',
                            component: resolve => require(['@/views/admin/sitesettings/PrivacyPolicy.vue'], resolve),
                            meta: { requireAuth: true, index:0 },
                        },{
                            path: '/admin/sitesettings/Billing',
                            component: resolve => require(['@/views/admin/sitesettings/EditBilling.vue'], resolve),
                            meta: { requireAuth: true, index:1 },
                        },{
                            path: '/admin/sitesettings/Changeinvoice',
                            component: resolve => require(['@/views/admin/sitesettings/Changeinvoice.vue'], resolve),
                            meta: { requireAuth: true, index:2 },
                        }
                    ]
                },
                //ssp
                {
                    path: '/admin/bid',
                    name: 'ssp',
                    component: () => import('@/views/admin/SSP')
                },
                {
                    path: '/admin/ssp/list',
                    name: 'sspList',
                    component: () => import('@/views/admin/SSP/SSPList')
                },
                {
                    path: '/admin/ssp/add/:id',
                    name: 'sspAdd',
                    component: () => import('@/views/admin/SSP/AddSSP')
                }

            ],
        },

        {
            path: '/login',
            component: resolve => require(['@/views/account/Account.vue'], resolve),
            children: [
                {
                    path: '/',
                    component: resolve => require(['@/views/account/Login.vue'], resolve),
                },{
                    path: '/register',
                    component: resolve => require(['@/views/account/Register.vue'], resolve),
                    beforeEnter: (to, form, next) => {
                        NProgress.start();

                        if (localStorage.REGISTER && localStorage.REGISTER != 'undefined') {
                            next()
                        }else{
                            getIsRegister().then(res => {
                                let { data, code } = res.data || false;
                                if (code == 200 && data) {
                                    localStorage.REGISTER = data;
                                    next()
                                }else {
                                    next({path:'/login'}) 
                                }
                            }).catch(() => {
                                next({path:'/login'}) 
                            }).finally(() => {
                                NProgress.done();
                            })
                        }
                    }
                },{
                    path: '/password/:token',
                    component: resolve => require(['@/views/account/Password.vue'], resolve),
                }
            ]
        },

        {path: '*',redirect: '/404',hidden: true }

    ]
})

/*
* to 即将去往的路由
* form 当前离开的路由
* next  进行通过的钩子函数 参数： 1无参数表示通过 2:false 中断当前导游 3:{path:'/'} 指定页面
* */
router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {
        // 判断该路由是否需要登录权限
        if (window.sessionStorage.Authorization) {
            NProgress.start();
            // 通过vuex state获取当前的token是否存在 本项目判断toKen在api请求里处理
            next();
        } else {
            router.push("/login")
        }
    } else {
        next();
    }
})

/*
* 后置钩子
* */
router.afterEach(() => {
    NProgress.done();
});


export default router
